/* Author: Andrew Shore
 * CSCE 145 Section 004
 * Homework 10-This program creates a graphical user interface that reads from a file of instructions
 * and prints either the previous, current, or next instruction to the window and also speaks it out loud
 * using an imported speech class.
 */
import java.io.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.speech.Central;
import javax.speech.Engine;
import javax.speech.synthesis.Synthesizer;
import javax.speech.synthesis.SynthesizerModeDesc;
import javax.speech.synthesis.SynthesizerProperties;
import javax.speech.synthesis.Voice;
import java.awt.image.*;
import javax.imageio.ImageIO;


public class Homework10 extends JFrame implements ActionListener {
	
	private static String[] fileArray;
	//counter to keep track of array index in the actionPerformed method
	private int index;
	
	public static final int WIDTH=350;
	public static final int HEIGHT=250;
	private JLabel instructionLabel;
	private JTextField instructionPad;
	private JButton previous;
	private JButton current;
	private JButton next;
	private JButton exit;
	JLabel gamecock;
	
	private static Synthesizer synthesizer;
	private static Voice voice;
	
	
	
	private static final FlowLayout LAYOUT_STYLE=new FlowLayout();
	
	public Homework10(){

		super();
		super.setSize(WIDTH, HEIGHT);
		super.setTitle("Homework 10");
		super.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setup();
		
		//adds gamecock image to JFrame
		gamecock = new javax.swing.JLabel();
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        gamecock.setIcon(new javax.swing.ImageIcon("C:\\Documents and Settings\\Andrew S\\CSCE145\\Homework10\\gamecock.jpg"));
        add(gamecock);
        

		
		index=0;
		
		// Initializes speech function
		try{
		SynthesizerModeDesc desc = new SynthesizerModeDesc(
                null,          // engine name
                "general",     // mode name
                Locale.US,     // locale
                null,          // running
                null);         // voice
	    synthesizer = Central.createSynthesizer(desc);
	    // create the voice
        String voiceName = System.getProperty("voiceName", "kevin16");
        voice = new Voice
            (voiceName, Voice.GENDER_DONT_CARE, Voice.AGE_DONT_CARE, null);

        // get it ready to speak
        synthesizer.allocate();
        synthesizer.resume();
        synthesizer.getSynthesizerProperties().setVoice(voice);
		}
		catch(Exception e){
			e.printStackTrace();
		}
	}
	
	//sets up GUI
	public void setup(){
		
		instructionLabel=new JLabel("Instruction");
		
		this.instructionPad=new JTextField(20);
		this.instructionPad.addActionListener(this);
		
		this.previous=new JButton("Previous Instruction");
		this.previous.addActionListener(this);
		previous.setBackground(Color.YELLOW);
		
		this.current=new JButton("Current Instruction");
		this.current.addActionListener(this);
		current.setBackground(Color.YELLOW);
		
		this.next=new JButton("Next Instruction");
		this.next.addActionListener(this);
		next.setBackground(Color.YELLOW);
		
		this.exit=new JButton("Exit Program");
		this.exit.addActionListener(this);
		exit.setBackground(Color.YELLOW);
		
		
		
		Container container = this.getContentPane();
		
		container.setLayout(LAYOUT_STYLE);
		container.add(instructionLabel);
		container.add(instructionPad);
		container.add(previous);
		container.add(current);
		container.add(next);
		container.add(exit);			
		
	}
	
	
	public void actionPerformed(ActionEvent event){
		
		if(event.getActionCommand().equals("Next Instruction")){
			index++;
			
			if(fileArray[index]!=null){
				//prints the next instruction to the screen
			this.instructionPad.setText(fileArray[index]);
			
				//speaks the next instruction out loud
			synthesizer.speakPlainText(fileArray[index], null);
			}
		}
		else if(event.getActionCommand().equals("Current Instruction")){
			
			if(fileArray[index]!=null){
				//prints the next instruction to the screen
			this.instructionPad.setText(fileArray[index]);
				//speaks the current instruction out loud
			synthesizer.speakPlainText(fileArray[index], null);
			}
		}
		else if(event.getActionCommand().equals("Previous Instruction")){
			if(index!=0){
				index--;
				if(fileArray[index]!=null){
					//prints the previous instruction to the screen
				this.instructionPad.setText(fileArray[index]);
					//speaks the previous instruction to the screen
				synthesizer.speakPlainText(fileArray[index], null);
				}
				}
		}
		else if(event.getActionCommand().equals("Exit Program")){
			synthesizer.speakPlainText("Thank you for using the program. Goodbye.", null);
			JOptionPane.showMessageDialog(null, "Thank you for using the program. Goodbye.");
			
			System.exit(0);
		}
	}
	
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		//initializes array
		fileArray=new String[50];
		
		//Asks user for a file to read from
		Scanner keyboard=new Scanner(System.in);
		
		boolean haveFile=false;
		while(!haveFile){
		
		try{
		String input=JOptionPane.showInputDialog(null, "Welcome to the program please enter the name of the file you want to read from:");

		File theFile=new File(input);
		Scanner inputStream=new Scanner(theFile);
		
		//assigns the lines of the file to an index in the array
		for(int index=0; index<fileArray.length;index++){
			if(inputStream.hasNextLine()){
				fileArray[index]=inputStream.nextLine();
			}
				
		}
		//closes input stream
		inputStream.close();
		//file has been found and input stream created so loop is ended
		haveFile=true;
		}
		
		//file not found so haveFile is still false	
		catch(FileNotFoundException fnfe){
			JOptionPane.showMessageDialog(null, "File Not Found. Please try again.");
			
		}
		}
		
		//creates GUI Frame
		Homework10 theFrame=new Homework10();
		
		theFrame.setVisible(true);
		
		}

	

	
		
	}


